package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.BusinessBranchBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.BusinessBranchMapper;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.ValidationKey;
import com.mysql.cj.util.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 营业网点-编辑
 */
public class BusinessBranchUpdate implements ServerInterface {

	private static Logger logger = LogManager.getLogger(BusinessBranchUpdate.class);

	@Override
	@ValidationKey(clazz = BusinessBranchBean.class,method="update")
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			BusinessBranchMapper mapper = factory.getMapper(BusinessBranchMapper.class);
			BusinessBranchBean bean = JsonUtils.objectToPojo(req.getData(), BusinessBranchBean.class);
			// 校验字段重复
			if (mapper.check2("branch_name", bean.getBranchName(),bean.getId()) > 0) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "营业网点名称：" + bean.getBranchName() + "的信息已存在。",
						void.class);
			}
			if (mapper.check2("branch_no", bean.getBranchNo(),bean.getId()) > 0) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "营业网点编号：" + bean.getBranchNo() + "的信息已存在。",
						void.class);
			}
			if (StringUtils.isNullOrEmpty(bean.getId())) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "id为空", void.class);
			}
			bean.setUpdateInfo(userInfo);
			mapper.updateBusinessBranch(bean);
		} catch (Exception e) {
			logger.error("修改营业网点失败", e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "修改营业网点失败", void.class);
		}
		return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
	}

}
